<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateProjects extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('projects', function (Blueprint $table) {
            $table->id()->from(config('migration.from'));

            $table->bigInteger('profile_id')->unsigned();
            $table->foreign('profile_id')->references('id')->on('profiles')->onDelete('cascade')->onUpdate('cascade');

            $table->string('title')->nullable();
            $table->text('description')->nullable();
            $table->boolean('is_draft')->default(true);
            $table->jsonb('rate')->default(json_encode([]));

            $table->bigInteger('image_id')->unsigned()->nullable()->comment('Фото проекта');
            $table->foreign('image_id')->references('id')->on('files')->onDelete('set null');

            $table->jsonb('medias')->default(json_encode([]));
            $table->index('medias', null, 'gin');

            $table->jsonb('videos')->default(json_encode([]));

            $table->string('stage_key')->unsigned()->comment('Стадия проекта');
            $table->foreign('stage_key')->references('key')->on('types')->onDelete('restrict')->onUpdate('restrict');

            $table->string('moderation_status_key')->unsigned()->comment('Статус модерации');
            $table->foreign('moderation_status_key')->references('key')->on('statuses')->onDelete('restrict')->onUpdate('restrict');

            $table->timestamp('moderated_at')->nullable();

            $table->bigInteger('company_id')->unsigned();
            $table->foreign('company_id')->references('id')->on('project_companies');

            $table->bigInteger('condition_id')->unsigned();
            $table->foreign('condition_id')->references('id')->on('project_conditions');

            $table->bigInteger('analytic_id')->unsigned();
            $table->foreign('analytic_id')->references('id')->on('project_analytics');

            $table->bigInteger('government_id')->unsigned()->nullable();
            $table->foreign('government_id')->references('id')->on('project_governments');

            $table->jsonb('businesses')->default(json_encode([]));

            $table->string('project_rating_key')->unsigned();
            $table->foreign('project_rating_key')->references('key')->on('types')->onDelete('restrict')->onUpdate('restrict');

            $table->string('profile_rating_key')->unsigned();
            $table->foreign('profile_rating_key')->references('key')->on('types')->onDelete('restrict')->onUpdate('restrict');

            $table->string('document_rating_key')->unsigned();
            $table->foreign('document_rating_key')->references('key')->on('types')->onDelete('restrict')->onUpdate('restrict');

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('projects');
    }
}
